package org.jef.socket.event;

import lombok.AllArgsConstructor;
import org.jef.socket.msg.WebSocketMsg;
import org.jef.socket.websocket.IWebSocket;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @description: websocket事件监听器
 * @author: ChengZhi 0336
 * @create: 2024-08-23 14:47
 **/
@Component
@AllArgsConstructor
public class WebSocketEventListener implements InitializingBean {

    private final IWebSocket webSocket;

    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Async
    @EventListener(WebSocketAsyncEvent.class)
    public void webSocketAsyncEvent(WebSocketAsyncEvent event) {
        EventMsg source = (EventMsg) event.getSource();
        webSocket.send(EventType.TOPIC_WEBSOCKET, new WebSocketMsg(source.getMsg(), source.getRoomId()));
    }

    @EventListener(WebSocketSyncEvent.class)
    public void webSocketSyncEvent(WebSocketSyncEvent event) {
        EventMsg source = (EventMsg) event.getSource();
        webSocket.send(EventType.TOPIC_WEBSOCKET, new WebSocketMsg(source.getMsg(), source.getRoomId()));
    }
}
